package ru.avn.devtool4j.spi.writers.impl;

public class VariableHelper {

	public static boolean isPrimitive(Object object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		Class<?> type = object.getClass();
		
		return String.class.isAssignableFrom(type) || 
				Number.class.isAssignableFrom(type) || 
				Boolean.class.isAssignableFrom(type) ||
				Character.class.isAssignableFrom(type);
		
	}
	
	
	public static String returnValueOfCode(String object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("\"")
			.append(object.toString())
			.append("\"")
			.append("")
			.toString();
		
	}
	
	
	public static String returnValueOfCode(Integer object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("Integer.valueOf(")
			.append(object.toString())
			.append(")")
			.append("")
			.toString();
		
	}
	
	
	public static String returnValueOfCode(Long object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("Long.valueOf(")
			.append(object.toString())
			.append("L)")
			.append("")
			.toString();
		
	}
	
	public static String returnValueOfCode(Short object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("Short.valueOf((short)")
			.append(object.toString())
			.append(")")
			.append("")
			.toString();
		
	}
	
	public static String returnValueOfCode(Double object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("Double.valueOf(")
			.append(object.toString())
			.append(")")
			.append("")
			.toString();
		
	}
	
	public static String returnValueOfCode(Float object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		return new StringBuilder("")
			.append("Float.valueOf(")
			.append(object.toString())
			.append("f)")
			.append("")
			.toString();
		
	}
	
	
	public static String returnValueOfCode(Boolean object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}

		return new StringBuilder("")
			.append("Boolean.valueOf(")
			.append(object.toString())
			.append(")")
			.append("")
			.toString();
		
	}
	
	public static String returnValueOfCode(Character object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}

		return new StringBuilder("")
			.append("Character.valueOf('")
			.append(object.toString())
			.append("')")
			.append("")
			.toString();
		
	}
	
	public static String returnValueOfCode(Object object) {
		if (object == null) {
			throw new IllegalArgumentException();
		}
		
		Class<?> type = object.getClass();
		
		if (String.class.isAssignableFrom(type)) {
			return returnValueOfCode((String) object);
		} else if (Integer.class.isAssignableFrom(type)) {
			return returnValueOfCode((Integer) object);
		}  else if (Long.class.isAssignableFrom(type)) {
			return returnValueOfCode((Long) object);
		}  else if (Short.class.isAssignableFrom(type)) {
			return returnValueOfCode((Short) object);
		}  else if (Double.class.isAssignableFrom(type)) {
			return returnValueOfCode((Double) object);
		}  else if (Float.class.isAssignableFrom(type)) {
			return returnValueOfCode((Float) object);
		}  else if (Boolean.class.isAssignableFrom(type)) {
			return returnValueOfCode((Boolean) object); 
		}  else if (Character.class.isAssignableFrom(type)) {
				return returnValueOfCode((Character) object);
		} else {
			throw new UnsupportedOperationException("unsupported type " + type);
		}
	
	}	
	
}
